import pytest
import requests


@pytest.fixture
def base_url():
    return "http://127.0.0.1:8080"


def test_add_region_type_success(base_url):
    type_data = {"type": "string"}
    response = requests.post(
        f"{base_url}/region/types", json=type_data, cookies={"id": "2"}
    )
    assert response.status_code == 201
    assert "id" in response.json()
    assert response.json()["type"] == type_data["type"]


@pytest.mark.parametrize("type_value", [None, "", "   "])
def test_add_region_type_invalid_input(base_url, type_value):
    type_data = {"type": type_value}
    response = requests.post(
        f"{base_url}/region/types", json=type_data, cookies={"id": "2"}
    )
    assert response.status_code == 400


def test_add_region_type_unauthorized(base_url):
    type_data = {"type": "string"}
    response = requests.post(f"{base_url}/region/types", json=type_data)
    assert response.status_code == 401


def test_add_region_type_conflict(base_url):
    type_data = {"type": "string"}
    response = requests.post(
        f"{base_url}/region/types", json=type_data, cookies={"id": "2"}
    )
    assert response.status_code == 409


def test_get_region_type_success(base_url):
    response = requests.get(f"{base_url}/region/types/1", cookies={"id": "2"})
    assert response.status_code == 200
    assert response.json() == {"id": 1, "type": "string"}


@pytest.mark.parametrize("type_id", [None, 0, -1])
def test_get_region_type_invalid_input(type_id, base_url):
    response = requests.get(f"{base_url}/region/types/{type_id}", cookies={"id": "2"})
    assert response.status_code == 400


def test_get_region_type_unauthorized(base_url):
    response = requests.get(f"{base_url}/region/types/1")
    assert response.status_code == 401


def test_get_region_type_not_found(base_url):
    response = requests.get(f"{base_url}/region/types/999", cookies={"id": "2"})
    assert response.status_code == 404
